// pages/swxq/swxq.js
const app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
      listArr: [],
      foundid:0,
      userId:0,
      listArr: [],
      comment_list: 0,
      userId: -1,
      input:'',
      user:{}
    },
    adddetial: function () {
      var that = this;
      wx.request({
        url: 'https://www.woodymidnight.cn//BCUHelper/api/found/updatafoundstate',
        method : 'POST',
        data:{
          foundId: this.data.foundid
        },
        header: { 'content-type': 'application/x-www-form-urlencoded' }, 
        success: function (res) {   
          var resData = res.data;
          console.log("数据" , resData);
          console.log("this:",this);
          that.setData({
            listArr: resData.data
          })
        }
      })
      wx.navigateBack({
        delta: 1,
      })
    },
    chaxun(){
        var that=this;
      wx.request({  
        url: 'https://www.woodymidnight.cn/BCUHelper/api/found/selectfoundbyid',  
        method: 'POST',
        data:{
            foundId : this.data.foundid
        },                                      
        header: { 'content-type': 'application/x-www-form-urlencoded' }, 
        success: function (res) {   
          var resData = res.data;
          console.log("数据" , resData);
          console.log("this:",this);
          that.setData({
            listArr: resData.data
          })
        }
      })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options);
        var foundid = parseInt(options.foundid);
        this.setData({
            foundid: foundid,
            userId: app.data.userInfo.userid,
            user: app.data.userInfo
        })
        this.chaxun();
        this.selectComment()
    },

    input: function(res){
      this.setData({
        input: res.detail.value
      })
    },

    selectComment(){
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/comment/selectbytypeandid',
        method: 'POST',
        header: { 
          'content-type': 'application/x-www-form-urlencoded' 
        }, 
        data: {
          comType: 1,
          comObjId: this.data.foundid
        },
        success: res =>{
          console.log(res);
          this.setData({
            comment_list: res.data.data
          })
        }
      })
    },

    del: function(e) {
      var a = this;
      console.log(e.currentTarget.dataset.id), 
      wx.showModal({
          content: "确认删除该评论吗？",
          success: res => {
            if(res.confirm){
              wx.request({
                url: 'https://www.woodymidnight.cn/BCUHelper/api/comment/delbycomid',
                method: 'POST',
                data:{
                  comId : e.currentTarget.dataset.id
                },
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: res => {
                  a.selectComment()
                }
              })
            }else if (res.cancel) {
  
            }
          }
      });
    },

    addlike(e){
      var that = this;
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/found/updatafoundlike',
        method: 'POST',
        data:{
          foundId : this.data.foundid
        },
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: function (res) {   
          var resData = res.data;
          console.log("数据" , resData);
          console.log("this:",this);
          that.setData({
            listArr: resData.data
          })
        }
      })
    },

    submit: function(res){
      // console.log(this.data.circleid);
      wx.request({
        url: 'https://www.woodymidnight.cn/BCUHelper/api/comment/insert',
        method: 'POST',
        data: {
          comcontent: this.data.input,
          comobjid: this.data.foundid,
          comtype: 1,
          comuserid: this.data.user.userid,
          comuserimage: this.data.user.userimage,
          comusername: this.data.user.username
        },
        header: { 'content-type': 'application/x-www-form-urlencoded' },
        success: res =>{
          console.log(res);
          this.setData({
            input: ''
          })
          this.selectComment()
        }
      })
    }
})